/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd
 ******************************************************************************
 *  @File Name        : kf8a100fxx_wdg.c
 *  @Author           : ChipON AE/FAE Group
 *  @Date             : 2023-12-19
 *  @HW Version       : KF8A100Fxx
 *  @Lib Version      : V2.0.1
 *  @Description      : This file provides the driver for the wdg module
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd
 *  All rights reserved.
 *
 *  This software is copyright protected and proprietary to
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.
 *****************************************************************************/
/******************************************************************************
 *  |Date        |Version  |Author       |Description
 ******************************************************************************
 *  |2023-12-25  |V2.0     |Xiong Yichao |New creat
 *****************************************************************************/

/******************************************************************************
 **                     QAC Warnings
 *****************************************************************************/
/* PRQA S 0436 EOF #8-bit MCU Features: Direct Write Registers. */
/* PRQA S 1291, 1820, 1821, 1822, 1823, 1851, 1853, 3715, 3757, 3760 EOF #Registers are essentially unsigned numbers. */
/* PRQA S 2754 EOF #Level 7 problem due to unsupported assembly instructions. */
/* PRQA S 3768, 3771, 4130, 4393, 4434, 4443, 4446, 4461, 4532 EOF #Registers are essentially unsigned numbers. */

/******************************************************************************
 *                      Include Files
 *****************************************************************************/
#include "kf8a100fxx_wdg.h"
/******************************************************************************
 *                      Macro Definitions
 *****************************************************************************/

/******************************************************************************
 *                      Constants
 *****************************************************************************/

/******************************************************************************
 *                      Type definitions
 *****************************************************************************/

/******************************************************************************
 *                      Variables
 *****************************************************************************/

/******************************************************************************
 *                      Function definitions
 *****************************************************************************/

/**
 * @brief WDG initialize TWDT = (2^(WDTPS+5)*2^(PS))/(31250)
 *
 * @param Pre WDG prescale
 * @param Div WDT Div
 */
void Wdg_Init(Wdg_WdtpsType Pre, Wdg_PsType Div)
{
    uint8_t reg_temp = 0u;
    WDTPS            = (uint8_t)Pre;
    if (Div < WDG_PS_NONE)
    {
        /* PRQA S 3109,3139 1 #assembly syntax. */
        _CWDT();
        PSA      = 1u;
        reg_temp = ((uint8_t)OPTR & 0xf8u) | (uint8_t)Div;
        OPTR     = reg_temp;
        _CWDT();
    }
    else
    {
        _CWDT();
        PSA = 0u;
        _CWDT();
    }
    SWDTEN = 1u;
}

/**
 * @brief WDG deinitialization
 *
 */
void Wdg_DeInit(void)
{
    SWDTEN = 0u;
    WDTPS  = 0x04u;
    if (PSA == 1u)
    {
        _CWDT();
        PS0 = 1u;
        PS1 = 1u;
        PS2 = 1u;
        _CWDT();
    }
    else
    {
        /* nothing */
    }
}

/**
 * @brief watch dog enable, This function is invalid if WDG is turned on in the configuration word
 *
 */
void Wdg_Enable(void)
{
    SWDTEN = 1u;
}

/**
 * @brief watch dog disable, This function is invalid if WDG is turned on in the configuration word
 *
 */
void Wdg_Disable(void)
{
    SWDTEN = 0u;
}

/**
 * @brief feed watch dog
 *
 */
void Wdg_FeedDog(void)
{
    /* PRQA S 3109,3139 1 #assembly syntax. */
    _CWDT();
}
/* EOF */
